package com.wzdigit.wms.wms.controller.across;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.framework.Shift;
import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.client.request.across.oms.OrderCloseInputDto;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.across.AcrossResult;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.dto.wms2oms.GdnCntrInfoDto;
import com.wzdigit.wms.wms.service.across.OmsService;
import com.wzdigit.wms.wms.service.core.GdnManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 删改单控制-OMS删改单控制  前端控制器
 *
 * @author yangdongjie
 * @CarrierController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "OMS接口层")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class OmsController {

    @Autowired
    private OmsService omsService;

    @Autowired
    private GdnManagerService gdnManagerService;

    private static final String REQUEST_MODEL = "/across/oms";

    /**
     * OMS删改单控制
     *
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "OMS删改单控制", notes = "OMS删改单控制")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/orderClose")
    public AcrossResult<OrderCloseInputDto> orderClose(
            @RequestBody OrderCloseInputDto dto,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return omsService.orderClose(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "获取单据对应的柜号编码", notes = "获取单据对应的柜号编码")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/getCntrInfoByNo")
    public Result<GdnCntrInfoDto> getCntrInfoByNo(String billNo) {
        try {
            if (billNo.equals("")) {
                Shift.fatal("请传入单号!");
            }
            QueryWrapper<GdnHeader> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("GDN_NO", billNo).or().eq("SOURCE_SHIP_NOTICE", billNo);
            queryWrapper.select("GDN_NO", "SOURCE_SHIP_NOTICE", "CNTR_NO");
            List<GdnHeader> gdnHeaders = this.gdnManagerService.getGdnHeaderMapper().selectList(queryWrapper);
            if (gdnHeaders.size() == 0) {
                Shift.fatal("没有找到单据信息!");
            }
            GdnHeader gdnHeader = gdnHeaders.get(0);
            GdnCntrInfoDto dto = new GdnCntrInfoDto();
            dto.setGdnNo(gdnHeader.getGdnNo());
            dto.setSourceNo(gdnHeader.getSourceShipNotice());
            dto.setCntrNo(gdnHeader.getCntrNo());
            return new Result<>(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            return new Result<>(false, e.getContent());
        }
    }
}
